package sangpil.media
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Video;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	
	import sangpil.events.MoviePlayerEvent;
	
	public class DesignedMoviePlayer extends MoviePlayer
	{
		private var controller:Navi_VideoPlayerClip;
		private var fileRef:FileReference;
		private var flvFilter:FileFilter;
		public function DesignedMoviePlayer(vURL:String=null)
		{
			super(vURL);
			this.setLayout();	
		}
		
		private function setLayout():void
		{
			this.controller = new Navi_VideoPlayerClip();
			this.addChild(this.controller);
			
			this.flvFilter = new FileFilter("Flv File", "*.flv");
			
			this.fileRef = new FileReference();
			this.fileRef.addEventListener(Event.SELECT, onSelect);
			
			this.controller.btn_browse.buttonMode = true;
			
			this.controller.bar_progress.btn_progress.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.controller.btn_play.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.controller.btn_pause.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.controller.btn_browse.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}
		
		private function onDown(e:MouseEvent):void
		{
			switch(e.currentTarget.name)
			{
				case this.controller.bar_progress.btn_progress.name:
					//trace(this.metaData.duration*e.currentTarget.mouseX/100, this.metaData.duration, e.currentTarget.mouseX);
					this.removeEventListener(Event.ENTER_FRAME, this.throwCurrentTime);
					e.currentTarget.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
					e.currentTarget.addEventListener(MouseEvent.MOUSE_UP, onUp);
					e.currentTarget.addEventListener(MouseEvent.MOUSE_OUT, onOut);
					this.controller.bar_progress.progressBar.width = e.currentTarget.mouseX;
					this.stream.seek(this.metaData.duration*e.currentTarget.mouseX/100);
					this.stream.pause();
				break;
				case this.controller.btn_play.name:
					trace(this.stream.time, this.metaData.duration);
					if(Math.abs(this.stream.time - this.metaData.duration) < 1)
					{
						this.stream.seek(0.1);
						this.stream.resume();
						this.addEventListener(Event.ENTER_FRAME, this.throwCurrentTime);
					}else{
						this.stream.resume();
					}
				break;
				case this.controller.btn_pause.name:
					this.stream.pause();
				break;
				case this.controller.btn_browse.name:
					this.stream.pause();
					this.fileRef.browse([this.flvFilter]);
					this.dispatchEvent(new MoviePlayerEvent(this.metaData, MoviePlayerEvent.VIDEO_CHANGING));
				break;
			}
		}
		
		private function onSelect(e:Event):void
		{
			var f:FileReference = e.currentTarget as FileReference;
			//trace(f.data, f.name);
			this.changeUrl("./flvAsset/"+f.name);
			this.controller.txt_url.text = "./flvAsset/"+f.name;
			this.dispatchEvent(new MoviePlayerEvent(this.metaData, MoviePlayerEvent.VIDEO_CHANGED));
		}
		
		private function onUp(e:MouseEvent):void
		{
			this.addEventListener(Event.ENTER_FRAME, this.throwCurrentTime);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			this.stream.resume();
		}
		
		private function onOut(e:MouseEvent):void
		{
			this.addEventListener(Event.ENTER_FRAME, this.throwCurrentTime);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
			this.stream.resume();
		}
		
		private function onMove(e:MouseEvent):void
		{
			this.controller.bar_progress.progressBar.width = e.target.mouseX;
			this.stream.seek(this.metaData.duration*e.target.mouseX/100);
		}
		
		override protected function nsMetaDataCallBack(mdata:Object):void
		{
			trace("MoviePlayer : Declare Video Instance");
        	this.metaData = mdata;
        	
        	if(this.video)
        	{
        		this.removeChild(this.video);
        		this.video = null;
        	}
        	this.video = new Video(mdata.width, mdata.height);
            video.attachNetStream(this.stream);
            this.addChildAt(video,0);
            this.stream.seek(0.1);

            this.addEventListener(Event.ENTER_FRAME, this.throwCurrentTime);
            this.dispatchEvent(new MoviePlayerEvent(this.metaData, MoviePlayerEvent.VIDEO_ADDED));
            
            this.controller.x = this.video.x;
            this.controller.y = this.video.height+1;
            this.controller.bar_progress.width = this.video.width - this.controller.bar_progress.x;
            this.controller.bg_second.width = this.controller.btn_browse.width = this.video.width;
            
        	this.controller.txt_url.text = this.videoURL;
		}
		
		override protected function throwCurrentTime(e:Event):void
		{
			var obj:Object = {current:this.stream.time, total:this.metaData.duration};
			this.dispatchEvent(new MoviePlayerEvent(obj, MoviePlayerEvent.VIDEO_PLAYING));
			
			this.controller.bar_progress.progressBar.width = this.stream.time/this.metaData.duration*100;
		}
	}
}